
//线段连接，将同层的线条连接在一起

class RLines {
    constructor(s, lines) {
        this.s = s;
        this.lines = lines;
    }
    
    pointToLine(p, b) {
        var d,dis,res = {b:false,c:false,ps:false}
        for (var i = 0; i < b.length - 1; i++) {

            var x = 0, c = 0, begin = 0, end = 0

            if(b[i].x == b[i + 1].x){
                if(Math.min(b[i].y,b[i + 1].y) < p.y && Math.max(b[i].y,b[i + 1].y) > p.y){
                    begin = Math.abs(b[i].x - p.x)
                    end = begin
                    if(begin <= this.s){
                        res = {b:true,c:true}
                        return res
                    }
                }
                else{
                    begin = Math.sqrt(Math.pow(b[i].x - p.x, 2) + Math.pow(b[i].y - p.y, 2))
                    end = Math.sqrt(Math.pow(b[i + 1].x - p.x, 2) + Math.pow(b[i + 1].y - p.y, 2))

                    if(begin <= this.s){
                        res.b = true
                        res.ps = true
                        return res
                    }
                    else if(end <= this.s){
                        res.b = true
                        return res
                    }
                }
                
            }
            else{
                
                begin = Math.sqrt(Math.pow(b[i].x - p.x, 2) + Math.pow(b[i].y - p.y, 2))
                end = Math.sqrt(Math.pow(b[i + 1].x - p.x, 2) + Math.pow(b[i + 1].y - p.y, 2))
                dis = Math.sqrt(Math.pow(b[i].x - b[i + 1].x, 2) + Math.pow(b[i].y - b[i +  1].y, 2))
                if(begin <= this.s){
                    res.b = true
                    res.ps = true
                    return res
                }
                else if(end <= this.s){
                    res.b = true
                    return res
                }
               //console.log(begin,end,dis)
                var anga = (Math.pow(begin,2) + Math.pow(dis,2) - Math.pow(end,2)) / (2 * begin * dis)
                var angb = (Math.pow(end,2) + Math.pow(dis,2) - Math.pow(begin,2)) / (2 * end * dis)
                //console.log('ang:',anga,angb)
                anga = anga.toFixed(4)
                angb = angb.toFixed(4)
                if(anga * angb <= -1){
                    //console.log("dd")
                }
                else if(Math.abs(anga) < 1 && (Math.acos(anga) > Math.PI / 2)){
                    //begin = dis
                }
                else if(Math.abs(angb) < 1 && (Math.acos(angb) > Math.PI / 2)){
                    //begin = dis
                }
                else{
                    
                    x = (b[i].y - b[i + 1].y) / (b[i].x - b[i + 1].x)
                    c = b[i].y - b[i].x * x
                    begin = Math.abs(p.x * x - p.y + c) / Math.sqrt(Math.pow(x, 2) + 1)
                    if(begin <= this.s){
                        res = {b:true,c:true}
                        return res
                    }
                }
            }
            //if(begin < 100)
            //console.log(begin,end)
        }
        
        return res
    }
    lineToLine(a, c,t) {
        var m,b = []
        b = b.concat(c)
        if(t && t == true){
            b.push(c[0])
        }
        m = this.pointToLine(a[0],b)
        if(m.b){
            m.l = 0
            return m
        }
    
        m = this.pointToLine(a[a.length - 1],b)
        if(m.b){
            m.l = 1
            return m
        }
    
        m = this.pointToLine(b[0],a)
        if(m.b){
            m.l = 2
            return m
        }
    
        m = this.pointToLine(b[b.length - 1],a)
        if(m.b){
            m.l = 3
            return m
        }
        
        return m
    }
    renderByIndex(id, a) {
        var m
        // for (var i = id; i < a.length - 1; i++) {
        //        if((a[i + 1].userData.id == 114 && a[id].userData.id == 324) || (a[i + 1].userData.id == 324 && a[id].userData.id == 114)){
        //            //console.log(a[id].vertices, a[i + 1].vertices)
        //            m = this.lineToLine(a[id].vertices, a[i + 1].vertices,a[i + 1].shape)
        //             //if(m.b){
        //             console.log(a[id],a[i + 1],m)
        //             //}
        //        }
        // }
        // return 
        for (var i = id; i < a.length - 1; i++) {
      
            m = this.lineToLine(a[id].vertices, a[i + 1].vertices,a[i + 1].type)
            
            if(m.b){
                if(!a[id].userData.chr){
                    a[id].chr = []
                }
                if(!a[i + 1].userData.chr){
                    a[i + 1].chr = []
                }
                a[id].userData.chr.push(a[i + 1].userData.id)
                a[i + 1].userData.chr.push(a[id].userData.id)

                if(m.c){
                    switch(m.l){
                        case 0:
                            a[id].userData.p[0] += 2
                            break;
                        case 1:
                            a[id].userData.p[1] += 2
                            break;
                        case 2:
                            a[i+1].userData.p[0] += 2
                            break;
                        case 3:
                            a[i+1].userData.p[1] += 2
                            break;
                    }
                }
                else{
                    var ps = m.ps?0:1
                    switch(m.l){
                        case 0:
                            a[id].userData.p[0] ++
                            a[i+1].userData.p[ps] ++
                            break;
                        case 1:
                            a[id].userData.p[1] ++
                            a[i+1].userData.p[ps] ++
                            break;
                        case 2:
                            a[id].userData.p[ps] ++
                            a[i+1].userData.p[0] ++
                            break;
                        case 3:
                            a[id].userData.p[ps] ++
                            a[i+1].userData.p[1] ++
                            break;
                    }
                }
            }
            //console.log(this.lineToLine(a[id].vertices, a[i + 1].vertices))
        }
    }
    renderEntity(a) {
        //var res = [],len = a.length
        for (var i = 0; i < a.length; i++) {
            this.renderByIndex(i, a)
        }
    }
    //静态函数
    action(){
        //修改静态变量
        this.renderEntity(this.lines)
        //console.log(this.lines)
    }
}
module.exports = RLines;